package com.example.Sao.MyFragment;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.Date;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.MyFragment
 * @ClassName: SuccesDialog
 * @Description: 进行设备盘点
 * @Author: 西南科技大学ACM实验室阿毛
 * @CreateDate: 2020/4/24 12:57
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/24 12:57
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class SuccesDialog extends DialogFragment implements View.OnClickListener{
    private String deviceStreamCode;
    private String partment;
    private ObjectBoxHelper objectBoxHelper;
    private SuccesDialogLinster succesDialogLinster;
    private Context mContext;
    private AVLoadingIndicatorView indicatorView;
    private TextView mess;
    private ImageView succes;
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        objectBoxHelper = ObjectBoxHelper.getInstance();
        return super.onCreateDialog(savedInstanceState);
    }
    public void setSuccesDialogLinster(SuccesDialogLinster succesDialogLinster) {
        this.succesDialogLinster = succesDialogLinster;
    }
    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public void setDeviceStreamCode(String deviceStreamCode) {
        this.deviceStreamCode = deviceStreamCode;
    }

    public void setPartment(String partment) {
        this.partment = partment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.75);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.CENTER;
        //设置dialog的动画
        lp.windowAnimations = R.style.dialogAnim;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        View view = inflater.inflate(R.layout.fragment_succes_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        indicatorView = view.findViewById(R.id.IndicatorView);
        mess = view.findViewById(R.id.message);
        succes  =view.findViewById(R.id.succes_icon);
    }

    @Override
    public void onStart() {
        super.onStart();
        indicatorView.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                objectBoxHelper.RxQuery(deviceStreamCode.replaceAll("\r|\n", ""),
                        partment, DeviceEntity_.deviceStreamCode,DeviceEntity_.useDepartment).
                        subscribe(new Consumer<List<DeviceEntity>>() {
                    @Override
                    public void accept(List<DeviceEntity>entities) throws Exception {
                        if (entities.size()==1){
                            Date date = new Date(System.currentTimeMillis());
                            DeviceEntity deviceEntity = entities.get(0);
                            deviceEntity.setTime((date.getYear()+1900)+"-"+(date.getMonth()+1)+"-"+date.getDay());
                            deviceEntity.setStaus(true);
                            objectBoxHelper.remove(entities.get(0).getId());
                            objectBoxHelper.add(deviceEntity);
                            ChangeInfoDiagFragment changeInfoDiagFragment = new ChangeInfoDiagFragment();
                            changeInfoDiagFragment.setmContext(mContext);
                            changeInfoDiagFragment.setLbeanList(entities.get(0));
                            changeInfoDiagFragment.setSuccesDialogLinster(new SuccesDialogLinster() {
                                @Override
                                public void SuccesDialogLinster(boolean isSucces) {
                                      if (succesDialogLinster!=null){
                                          succesDialogLinster.SuccesDialogLinster(true);
                                      }
                                }
                            });
                            changeInfoDiagFragment.show(getFragmentManager(),"");
                            SuccesDialog.this.dismiss();
//                            indicatorView.hide();
//                            indicatorView.setVisibility(View.GONE);
//                            succes.setVisibility(View.VISIBLE);
//                            mess.setText("盘点成功");
//                            succes.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.succes));
//                            if (succesDialogLinster!=null){
//                                succesDialogLinster.SuccesDialogLinster(true);
//                            }
                        }else {
                            indicatorView.hide();
                            indicatorView.setVisibility(View.GONE);
                            succes.setVisibility(View.VISIBLE);
                            mess.setText("未查询到该设备");
                            succes.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.redworry));
                            if (succesDialogLinster!=null){
                                succesDialogLinster.SuccesDialogLinster(false);
                            }
                        }
                    }
                });
            }
        },600);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        indicatorView.hide();
    }
  public interface SuccesDialogLinster{
        public void SuccesDialogLinster(boolean isSucces);
  }

    //设置动画效果
//    private void initAnim() {
//        //通过加载XML动画设置文件来创建一个Animation对象；
//        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.item_anim);
//        //得到一个LayoutAnimationController对象；
//        LayoutAnimationController lac = new LayoutAnimationController(animation);
//        //设置控件显示的顺序；
//        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
//        //设置控件显示间隔时间；
//        lac.setDelay(0.3f);
//        //为ListView设置LayoutAnimationController属性；
//        list.setLayoutAnimation(lac);
//    }

}
